﻿using QD.LopPlat.UI.Code;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QD.LopPlat.UI
{
    public partial class RSAEncryption : Form
    {
        string _publicKey;
        public RSAEncryption()
        {
            InitializeComponent();

            this.MaximizeBox = false;
            this.FormBorderStyle = FormBorderStyle.FixedSingle;
            this.StartPosition = FormStartPosition.CenterScreen;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();

            //判断用户是否正确的选择了文件 
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                //获取用户选择文件的后缀名 
                string extension = Path.GetExtension(fileDialog.FileName);

                if (extension != ".spc")
                {
                    MessageBox.Show("文件类型错误，只能上传spc文件！");
                }
                else
                {
                    //获取用户选择的文件，并判断文件大小不能超过20K，fileInfo.Length是以字节为单位的 
                    FileInfo fileInfo = new FileInfo(fileDialog.FileName);
                    StreamReader reader = fileInfo.OpenText();
                    _publicKey = reader.ReadToEnd();
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string waitEncryptionStr = this.textBox1.Text.Trim();
            if (waitEncryptionStr.Length == 0)
                MessageBox.Show("请输入待加密字符！");
            else if (string.IsNullOrEmpty(_publicKey))
                MessageBox.Show("请选择密钥！");
            else
            {
                try
                {
                    string encryptionStr = ValidLicense.Instance.Encryption(_publicKey, waitEncryptionStr);
                    this.textBox2.Text = encryptionStr;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.textBox2.Text))
            {
                Clipboard.Clear();
                Clipboard.SetText(this.textBox2.Text);
                MessageBox.Show("复制成功！");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(_publicKey))
                MessageBox.Show("请选择密钥！");
            else
            {
                DateTime date = this.dateTimePicker1.Value.Date;
                string softId = this.textBox1.Text.Trim();
                if (softId.Length == 0)
                    MessageBox.Show("请输入待加密字符！");
                else
                {
                    int qtyuser = 3;
                    string _qtyuser = this.QtyUser.Text.Trim();
                    if (!string.IsNullOrEmpty(_qtyuser))
                    {
                        qtyuser = Convert.ToInt32(_qtyuser);
                    }

                    int qtyapply = 1;
                    string _qtyapply = this.QtyApply.Text.Trim();
                    if (!string.IsNullOrEmpty(_qtyapply))
                    {
                        qtyapply = Convert.ToInt32(_qtyapply);
                    }
                    string updateStr = softId + "," + qtyuser.ToString() + "," + qtyapply.ToString() + "," + date.ToShortDateString();

                    string encryptionStr = ValidLicense.Instance.Encryption(_publicKey, updateStr);
                    this.textBox3.Text = encryptionStr;
                }
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(_publicKey))
                MessageBox.Show("请选择密钥！");
            else
            {
                DateTime date = this.dateTimePicker1.Value.Date;
                string softId = this.textBox1.Text.Trim();
                if (softId.Length == 0)
                    MessageBox.Show("请输入待加密字符！");
                else
                {
                    string updateStr = softId + "_plat" + "," + date.ToShortDateString();

                    string encryptionStr = ValidLicense.Instance.Encryption(_publicKey, updateStr);
                    this.textBox3.Text = encryptionStr;
                }
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.textBox3.Text))
            {
                Clipboard.Clear();
                Clipboard.SetText(this.textBox3.Text);
                MessageBox.Show("复制成功！");
            }
        }
    }
}
